package com.zrz.controller;

import com.zrz.dao.auto.RedcodeMapper;
import com.zrz.domain.Image;
import com.zrz.model.auto.ImageMediaid;
import com.zrz.model.auto.User;
import com.zrz.param.UserParam;
import com.zrz.service.RedCodeService;
import com.zrz.service.UserService;
import com.zrz.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedCodeService redCodeService;
    @Autowired
    private WechatService wechatService;



    @RequestMapping("/detail")
    public User getUser(Integer id) {
        return userService.getUserInfo(id);
    }

    @RequestMapping("/invest")
    public List<User> getInvestUser(Integer id) {
        return userService.getInvestUser(id);
    }

    @RequestMapping("/vip")
    @ResponseBody
    public Map getRedCode(String code,HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user=userService.getUserById(userId);
        Map map=redCodeService.getRedCode(user,code);
        return map;
    }

    @RequestMapping("/image")
    @ResponseBody
    public List<ImageMediaid> image(String code, HttpServletRequest request){
        return wechatService.getMediaid();
    }

    @RequestMapping("/unionid")
    @ResponseBody
    public int getUserUnionId(){
         return userService.addUserUnionId();
    }
    @RequestMapping("/business")
    public String addBusiness(HttpServletRequest request,Model model){
        Integer uid=(Integer) request.getSession().getAttribute("userId");
        User user = userService.getUserById(uid);
//        User user = userService.getUserById(301);
        request.getSession().setAttribute("mallUser", user);
        request.getSession().setAttribute("userId", user.getId());
        model.addAttribute("mallUser",user);
        return "register";
    }

    @RequestMapping("/addBusSuccess")
    public String addBusSuccess(){
        return "addBusSuccess";
    }

}
